const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_open: 1,
    show_agreement: 0,
    is_agree: true,
    price_index : 'none',
    showGetHXCode: 0,
    navigator: { hide: 0 },
    fenxiangzhuan: {
      show: false,
      width: '86rpx;',
      height: '88rpx;',
      bottom: '380rpx;',
      top: '10rpx'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let that = this;
    const openid = app.globalData.openid;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid,
      openid: openid
    })
    app.ihttpGet("https://www.58zltc.com/api.php/Shop/getTCCPrice", function (res) {
      var data = res.data;
      var WxParse = require('../../../wxParse/wxParse.js');
      if (data.agree){
        var article = data.agree.content;
        WxParse.wxParse('article', 'html', article, that, 5);
      }
      that.setData({
        agree_str: data.agree,
        card: data.card,
        mycard: data.mycard,
        pic: data.pic
      });
    });
    //是否开过卡
    app.ihttpGet("https://www.58zltc.com/api.php/Shop/haveTCCard/openid/" + app.globalData.openid, function (res) {
      var data = res.data;
      that.setData({
        is_open: data.have_car,
        car_info: data.info,
        wid: data.wid
      })
    });
    //会员特权
    app.ihttpGet("https://www.58zltc.com/api.php/Shop/getVipRights", function (res) {
      console.log(res.data)
      that.setData({
        vipRights: res.data
      })
    });
    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator: v
        })
      },
    })

    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {

      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })
  },
  onReady: function () {
    app.confirmCommission()
  },
  pay: function (e) {
    
  },
  backto: function () {
    wx.navigateBack({});
  },
  checkboxChange: function (e) {
    this.setData({
      price_index: e.detail.value
    })
  },
  checkboxChangeAgree: function (e) {
    let that =this;
    var arr = e.detail.value;
    var a = true;
    arr[0] == 'agree' ? a = true : a = false;
    that.setData({
      is_agree: a
    })
  },
  show_agreement: function () {
    this.setData({
      show_agreement: 1
    })
  },
  hiden_agreement: function () {
    this.setData({
      show_agreement: 0
    })
  },
  openCard: function () {
    let that = this;
    var a = that.data.price_index;
    var b = that.data.is_agree;
    if (a == 'none') {
      wx.showToast({
        title: '请选择同城卡种类!',
        icon: 'none',
        duration: 1500
      })
    } else if (!b) {
      wx.showToast({
        title: '请阅读协议内容并同意!',
        icon: 'none',
        duration: 1500
      })
    } else {
      var post_data = {};
      post_data.openid = app.globalData.openid;
      post_data.cardid = that.data.card[a].id;//同城卡id
      app.ihttpPost("https://www.58zltc.com/api.php/Card/openCard", post_data, function (res) {
        if (res.data.err_code == 400) {
          app.iModel(res.data.err_msg);
        } else {
          var data = res.data;
          wx.requestPayment({
            'appId': data.appId,
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            'success': res => {
              if (res.errMsg == 'requestPayment:ok') {
                wx.navigateTo({
                  url: '/pages/shop/tccar/finish',
                })
              }
            }
          })
        }
      })
    }
  },
  getHXCode: function () {
    console.log(app.globalData.openid)
    var that = this;
    var a = that.data.showGetHXCode;
    a ? a = 0 : a = 1;
    this.setData({
      showGetHXCode: a
    })
  },
  jumpHXCode: function () {
    var that = this;
    wx.navigateTo({
      url: '/pages/shop/hexiao/hexiao?openid=' + app.globalData.openid + '&wid=' + that.data.wid,
    })
  },
  //快捷导航
  navigator: function (e) {
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1) {
      var offset = '0'
      var mum = 0;
    } else {
      var offset = width * 0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator: v
    })
  },
  changeFXZ: function () {
    var that = this;
    var a = that.data.fenxiangzhuan
    a.show ? a.show = false : a.show = true
    that.setData({
      fenxiangzhuan: a
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    return {
      title: '同城卡',
      path: 'pages/shop/tccar/tccar' + '?fenxiang=' + app.globalData.openid + '&fWid=' + app.globalData.wid,
      success: function (res) {
        var a = that.data.fenxiangzhuan
        a.show = false
        that.setData({
          fenxiangzhuan: a
        })
        // 转发成功
        wx.showToast({
          title: '转发成功！',
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
})